package com.aiti.lulian.utils;

import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;

public class PromotionCodeUtil {

    private static final String CHAR_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    private static final int BASE = CHAR_SET.length();

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        //murmurhash
        String hashCode = Hashing.murmur3_128().newHasher().putString("15689045009" + System.currentTimeMillis(), StandardCharsets.UTF_8).hash().toString();
//        String hashCode = Hashing.murmur3_32().newHasher().putString("15689045009" + System.currentTimeMillis(), StandardCharsets.UTF_8).hash().toString();
        System.out.println(hashCode);

        long number = 0;
        for (char character : hashCode.toCharArray()) {
            number = number * 256 + (int) character;
        }
        // 转换为62进制
        StringBuilder sb = new StringBuilder();
        while (number > 0) {
            int remainder = (int) (number % BASE);
            sb.append(CHAR_SET.charAt(remainder));
            number /= BASE;
        }
        String resCode = sb.reverse().toString();
        System.out.println(resCode);
    }

    /**
     * 生成六十二进制推广码
     * @param input 输入值
     */
    public static String generatePromotionCode_62(String input) {
        //murmurhash
        String hashCode = Hashing.murmur3_128().newHasher().putString(input, StandardCharsets.UTF_8).hash().toString();

        long number = 0;
        for (char character : hashCode.toCharArray()) {
            number = number * 256 + (int) character;
        }

        // 转换为62进制
        StringBuilder sb = new StringBuilder();
        while (number > 0) {
            int remainder = (int) (number % BASE);
            sb.append(CHAR_SET.charAt(remainder));
            number /= BASE;
        }

        return sb.reverse().toString();
    }
}